﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria.GameContent.UI.Elements;
using Terraria.GameInput;
using Terraria.Graphics;
using Terraria.Localization;
using Terraria.UI;
using Terraria.UI.Gamepad;

namespace Terraria.GameContent.UI.States
{
	// Token: 0x020003D8 RID: 984
	public class UIVirtualKeyboard : UIState
	{
		// Token: 0x17000284 RID: 644
		// (get) Token: 0x060022F2 RID: 8946 RVA: 0x00018A96 File Offset: 0x00016C96
		// (set) Token: 0x060022F3 RID: 8947 RVA: 0x00018AA3 File Offset: 0x00016CA3
		public string Text
		{
			get
			{
				return this._textBox.Text;
			}
			set
			{
				this._textBox.SetText(value);
				this.ValidateText();
			}
		}

		// Token: 0x17000285 RID: 645
		// (get) Token: 0x060022F4 RID: 8948 RVA: 0x00018AB7 File Offset: 0x00016CB7
		public static bool CanSubmit
		{
			get
			{
				return UIVirtualKeyboard._currentInstance != null && UIVirtualKeyboard._currentInstance._canSubmit;
			}
		}

		// Token: 0x17000286 RID: 646
		// (get) Token: 0x060022F5 RID: 8949 RVA: 0x00018ACC File Offset: 0x00016CCC
		public static int KeyboardContext
		{
			get
			{
				if (UIVirtualKeyboard._currentInstance == null)
				{
					return -1;
				}
				return UIVirtualKeyboard._currentInstance._keyboardContext;
			}
		}

		// Token: 0x060022F6 RID: 8950 RVA: 0x00473F20 File Offset: 0x00472120
		public UIVirtualKeyboard(string labelText, string startingText, UIVirtualKeyboard.KeyboardSubmitEvent submitAction, Action cancelAction, int inputMode = 0, bool allowEmpty = false)
		{
			this._keyboardContext = inputMode;
			this._allowEmpty = allowEmpty;
			UIVirtualKeyboard.OffsetDown = 0;
			this._lastOffsetDown = 0;
			this._edittingSign = (this._keyboardContext == 1);
			this._edittingChest = (this._keyboardContext == 2);
			UIVirtualKeyboard._currentInstance = this;
			this._submitAction = submitAction;
			this._cancelAction = cancelAction;
			this._textureShift = TextureManager.Load("Images/UI/VK_Shift");
			this._textureBackspace = TextureManager.Load("Images/UI/VK_Backspace");
			this.Top.Pixels = (float)this._lastOffsetDown;
			float num = (float)(-5000 * this._edittingSign.ToInt());
			float num2 = 255f;
			float precent = 0f;
			float num3 = 516f;
			UIElement uielement = new UIElement();
			uielement.Width.Pixels = num3 + 8f + 16f;
			uielement.Top.Precent = precent;
			uielement.Top.Pixels = num2;
			uielement.Height.Pixels = 266f;
			uielement.HAlign = 0.5f;
			uielement.SetPadding(0f);
			this.outerLayer1 = uielement;
			UIElement uielement2 = new UIElement();
			uielement2.Width.Pixels = num3 + 8f + 16f;
			uielement2.Top.Precent = precent;
			uielement2.Top.Pixels = num2;
			uielement2.Height.Pixels = 266f;
			uielement2.HAlign = 0.5f;
			uielement2.SetPadding(0f);
			this.outerLayer2 = uielement2;
			UIPanel uipanel = new UIPanel();
			uipanel.Width.Precent = 1f;
			uipanel.Height.Pixels = 225f;
			uipanel.BackgroundColor = new Color(23, 33, 69) * 0.7f;
			uielement.Append(uipanel);
			float num4 = -79f;
			this._textBox = new UITextBox("", 0.78f, true);
			this._textBox.BackgroundColor = Color.Transparent;
			this._textBox.BorderColor = Color.Transparent;
			this._textBox.HAlign = 0.5f;
			this._textBox.Width.Pixels = num3;
			this._textBox.Top.Pixels = num4 + num2 - 10f + num;
			this._textBox.Top.Precent = precent;
			this._textBox.Height.Pixels = 37f;
			base.Append(this._textBox);
			for (int i = 0; i < 10; i++)
			{
				for (int j = 0; j < 4; j++)
				{
					int index = j * 10 + i;
					UITextPanel<object> uitextPanel = this.CreateKeyboardButton("1234567890qwertyuiopasdfghjkl'zxcvbnm,.?"[index].ToString(), i, j, 1, true);
					uitextPanel.OnClick += this.TypeText;
					uipanel.Append(uitextPanel);
				}
			}
			this._shiftButton = this.CreateKeyboardButton("", 0, 4, 1, false);
			this._shiftButton.PaddingLeft = 0f;
			this._shiftButton.PaddingRight = 0f;
			this._shiftButton.PaddingBottom = (this._shiftButton.PaddingTop = 0f);
			this._shiftButton.BackgroundColor = new Color(63, 82, 151) * 0.7f;
			this._shiftButton.BorderColor = this._internalBorderColor * 0.7f;
			this._shiftButton.OnMouseOver += delegate(UIMouseEvent evt, UIElement listeningElement)
			{
				this._shiftButton.BorderColor = this._internalBorderColorSelected;
				if (this._keyState != UIVirtualKeyboard.KeyState.Shift)
				{
					this._shiftButton.BackgroundColor = new Color(73, 94, 171);
				}
			};
			this._shiftButton.OnMouseOut += delegate(UIMouseEvent evt, UIElement listeningElement)
			{
				this._shiftButton.BorderColor = this._internalBorderColor * 0.7f;
				if (this._keyState != UIVirtualKeyboard.KeyState.Shift)
				{
					this._shiftButton.BackgroundColor = new Color(63, 82, 151) * 0.7f;
				}
			};
			this._shiftButton.OnClick += delegate(UIMouseEvent evt, UIElement listeningElement)
			{
				Main.PlaySound(12, -1, -1, 1, 1f, 0f);
				this.SetKeyState((this._keyState == UIVirtualKeyboard.KeyState.Shift) ? UIVirtualKeyboard.KeyState.Default : UIVirtualKeyboard.KeyState.Shift);
			};
			UIImage uiimage = new UIImage(this._textureShift);
			uiimage.HAlign = 0.5f;
			uiimage.VAlign = 0.5f;
			uiimage.ImageScale = 0.85f;
			this._shiftButton.Append(uiimage);
			uipanel.Append(this._shiftButton);
			this._symbolButton = this.CreateKeyboardButton("@%", 1, 4, 1, false);
			this._symbolButton.PaddingLeft = 0f;
			this._symbolButton.PaddingRight = 0f;
			this._symbolButton.BackgroundColor = new Color(63, 82, 151) * 0.7f;
			this._symbolButton.BorderColor = this._internalBorderColor * 0.7f;
			this._symbolButton.OnMouseOver += delegate(UIMouseEvent evt, UIElement listeningElement)
			{
				this._symbolButton.BorderColor = this._internalBorderColorSelected;
				if (this._keyState != UIVirtualKeyboard.KeyState.Symbol)
				{
					this._symbolButton.BackgroundColor = new Color(73, 94, 171);
				}
			};
			this._symbolButton.OnMouseOut += delegate(UIMouseEvent evt, UIElement listeningElement)
			{
				this._symbolButton.BorderColor = this._internalBorderColor * 0.7f;
				if (this._keyState != UIVirtualKeyboard.KeyState.Symbol)
				{
					this._symbolButton.BackgroundColor = new Color(63, 82, 151) * 0.7f;
				}
			};
			this._symbolButton.OnClick += delegate(UIMouseEvent evt, UIElement listeningElement)
			{
				Main.PlaySound(12, -1, -1, 1, 1f, 0f);
				this.SetKeyState((this._keyState == UIVirtualKeyboard.KeyState.Symbol) ? UIVirtualKeyboard.KeyState.Default : UIVirtualKeyboard.KeyState.Symbol);
			};
			uipanel.Append(this._symbolButton);
			this.BuildSpaceBarArea(uipanel);
			this._submitButton = new UITextPanel<LocalizedText>((this._edittingSign || this._edittingChest) ? Language.GetText("UI.Save") : Language.GetText("UI.Submit"), 0.4f, true);
			this._submitButton.Height.Pixels = 37f;
			this._submitButton.Width.Precent = 0.4f;
			this._submitButton.HAlign = 1f;
			this._submitButton.VAlign = 1f;
			this._submitButton.PaddingLeft = 0f;
			this._submitButton.PaddingRight = 0f;
			this.ValidateText();
			this._submitButton.OnMouseOver += delegate(UIMouseEvent evt, UIElement listeningElement)
			{
				this.ValidateText();
			};
			this._submitButton.OnMouseOut += delegate(UIMouseEvent evt, UIElement listeningElement)
			{
				this.ValidateText();
			};
			this._submitButton.OnClick += delegate(UIMouseEvent evt, UIElement listeningElement)
			{
				string text = this.Text.Trim();
				if (text.Length > 0 || this._edittingSign || this._edittingChest || this._allowEmpty)
				{
					Main.PlaySound(10, -1, -1, 1, 1f, 0f);
					this._submitAction(text);
				}
			};
			uielement.Append(this._submitButton);
			this._cancelButton = new UITextPanel<LocalizedText>(Language.GetText("UI.Cancel"), 0.4f, true);
			this.StyleKey<LocalizedText>(this._cancelButton, true);
			this._cancelButton.Height.Pixels = 37f;
			this._cancelButton.Width.Precent = 0.4f;
			this._cancelButton.VAlign = 1f;
			this._cancelButton.OnClick += delegate(UIMouseEvent evt, UIElement listeningElement)
			{
				Main.PlaySound(11, -1, -1, 1, 1f, 0f);
				this._cancelAction();
			};
			uielement.Append(this._cancelButton);
			this._submitButton2 = new UITextPanel<LocalizedText>((this._edittingSign || this._edittingChest) ? Language.GetText("UI.Save") : Language.GetText("UI.Submit"), 0.72f, true);
			this._submitButton2.TextColor = Color.Silver;
			this._submitButton2.DrawPanel = false;
			this._submitButton2.Height.Pixels = 60f;
			this._submitButton2.Width.Precent = 0.4f;
			this._submitButton2.HAlign = 0.5f;
			this._submitButton2.VAlign = 0f;
			this._submitButton2.OnMouseOver += delegate(UIMouseEvent a, UIElement b)
			{
				((UITextPanel<LocalizedText>)b).TextScale = 0.85f;
				((UITextPanel<LocalizedText>)b).TextColor = Color.White;
			};
			this._submitButton2.OnMouseOut += delegate(UIMouseEvent a, UIElement b)
			{
				((UITextPanel<LocalizedText>)b).TextScale = 0.72f;
				((UITextPanel<LocalizedText>)b).TextColor = Color.Silver;
			};
			this._submitButton2.Top.Pixels = 50f;
			this._submitButton2.PaddingLeft = 0f;
			this._submitButton2.PaddingRight = 0f;
			this.ValidateText();
			this._submitButton2.OnMouseOver += delegate(UIMouseEvent evt, UIElement listeningElement)
			{
				this.ValidateText();
			};
			this._submitButton2.OnMouseOut += delegate(UIMouseEvent evt, UIElement listeningElement)
			{
				this.ValidateText();
			};
			this._submitButton2.OnClick += delegate(UIMouseEvent evt, UIElement listeningElement)
			{
				string text = this.Text.Trim();
				if (text.Length > 0 || this._edittingSign || this._edittingChest || this._allowEmpty)
				{
					Main.PlaySound(10, -1, -1, 1, 1f, 0f);
					this._submitAction(text);
				}
			};
			this.outerLayer2.Append(this._submitButton2);
			this._cancelButton2 = new UITextPanel<LocalizedText>(Language.GetText("UI.Cancel"), 0.72f, true);
			this._cancelButton2.TextColor = Color.Silver;
			this._cancelButton2.DrawPanel = false;
			this._cancelButton2.OnMouseOver += delegate(UIMouseEvent a, UIElement b)
			{
				((UITextPanel<LocalizedText>)b).TextScale = 0.85f;
				((UITextPanel<LocalizedText>)b).TextColor = Color.White;
			};
			this._cancelButton2.OnMouseOut += delegate(UIMouseEvent a, UIElement b)
			{
				((UITextPanel<LocalizedText>)b).TextScale = 0.72f;
				((UITextPanel<LocalizedText>)b).TextColor = Color.Silver;
			};
			this._cancelButton2.Height.Pixels = 60f;
			this._cancelButton2.Width.Precent = 0.4f;
			this._cancelButton2.Top.Pixels = 114f;
			this._cancelButton2.VAlign = 0f;
			this._cancelButton2.HAlign = 0.5f;
			this._cancelButton2.OnClick += delegate(UIMouseEvent evt, UIElement listeningElement)
			{
				Main.PlaySound(11, -1, -1, 1, 1f, 0f);
				this._cancelAction();
			};
			this.outerLayer2.Append(this._cancelButton2);
			UITextPanel<object> uitextPanel2 = this.CreateKeyboardButton("", 8, 4, 2, true);
			uitextPanel2.OnClick += delegate(UIMouseEvent evt, UIElement listeningElement)
			{
				Main.PlaySound(12, -1, -1, 1, 1f, 0f);
				this._textBox.Backspace();
				this.ValidateText();
			};
			uitextPanel2.PaddingLeft = 0f;
			uitextPanel2.PaddingRight = 0f;
			uitextPanel2.PaddingBottom = (uitextPanel2.PaddingTop = 0f);
			uitextPanel2.Append(new UIImage(this._textureBackspace)
			{
				HAlign = 0.5f,
				VAlign = 0.5f,
				ImageScale = 0.92f
			});
			uipanel.Append(uitextPanel2);
			UIText uitext = new UIText(labelText, 0.75f, true);
			uitext.HAlign = 0.5f;
			uitext.Width.Pixels = num3;
			uitext.Top.Pixels = num4 - 37f - 4f + num2 + num;
			uitext.Top.Precent = precent;
			uitext.Height.Pixels = 37f;
			base.Append(uitext);
			this._label = uitext;
			base.Append(uielement);
			this._textBox.SetTextMaxLength(this._edittingSign ? 99999 : 20);
			this.Text = startingText;
			if (this.Text.Length == 0)
			{
				this.SetKeyState(UIVirtualKeyboard.KeyState.Shift);
			}
			UIVirtualKeyboard.OffsetDown = 9999;
			this.UpdateOffsetDown();
		}

		// Token: 0x060022F7 RID: 8951 RVA: 0x00474998 File Offset: 0x00472B98
		private void BuildSpaceBarArea(UIPanel mainPanel)
		{
			Action createTheseTwo = delegate
			{
				bool flag2 = this.CanRestore();
				int x = flag2 ? 4 : 5;
				bool edittingSign = this._edittingSign;
				int num = (flag2 && edittingSign) ? 2 : 3;
				UITextPanel<object> uitextPanel = this.CreateKeyboardButton(Language.GetText("UI.SpaceButton"), 2, 4, (this._edittingSign || (this._edittingChest && flag2)) ? num : 6, true);
				uitextPanel.OnClick += delegate(UIMouseEvent evt, UIElement listeningElement)
				{
					Main.PlaySound(12, -1, -1, 1, 1f, 0f);
					this._textBox.Write(" ");
					this.ValidateText();
				};
				mainPanel.Append(uitextPanel);
				this._spacebarButton = uitextPanel;
				if (edittingSign)
				{
					UITextPanel<object> uitextPanel2 = this.CreateKeyboardButton(Language.GetText("UI.EnterButton"), x, 4, num, true);
					uitextPanel2.OnClick += delegate(UIMouseEvent evt, UIElement listeningElement)
					{
						Main.PlaySound(12, -1, -1, 1, 1f, 0f);
						this._textBox.Write(string.Concat('\n'));
						this.ValidateText();
					};
					mainPanel.Append(uitextPanel2);
					this._enterButton = uitextPanel2;
				}
			};
			createTheseTwo();
			bool flag = this.CanRestore();
			if (flag)
			{
				UITextPanel<object> restoreBar = this.CreateKeyboardButton(Language.GetText("UI.RestoreButton"), 6, 4, 2, true);
				restoreBar.OnClick += delegate(UIMouseEvent evt, UIElement listeningElement)
				{
					Main.PlaySound(12, -1, -1, 1, 1f, 0f);
					this.RestoreCancelledInput(this._keyboardContext);
					this.ValidateText();
					restoreBar.Remove();
					this._enterButton.Remove();
					this._spacebarButton.Remove();
					createTheseTwo();
				};
				mainPanel.Append(restoreBar);
				this._restoreButton = restoreBar;
			}
		}

		// Token: 0x060022F8 RID: 8952 RVA: 0x00018AE1 File Offset: 0x00016CE1
		private bool CanRestore()
		{
			if (this._edittingSign)
			{
				return UIVirtualKeyboard._cancelCacheSign.Length > 0;
			}
			return this._edittingChest && UIVirtualKeyboard._cancelCacheChest.Length > 0;
		}

		// Token: 0x060022F9 RID: 8953 RVA: 0x00474A40 File Offset: 0x00472C40
		private void TypeText(UIMouseEvent evt, UIElement listeningElement)
		{
			Main.PlaySound(12, -1, -1, 1, 1f, 0f);
			bool flag = this.Text.Length == 0;
			this._textBox.Write(((UITextPanel<object>)listeningElement).Text);
			this.ValidateText();
			if (flag && this.Text.Length > 0 && this._keyState == UIVirtualKeyboard.KeyState.Shift)
			{
				this.SetKeyState(UIVirtualKeyboard.KeyState.Default);
			}
		}

		// Token: 0x060022FA RID: 8954 RVA: 0x00474AB0 File Offset: 0x00472CB0
		public void SetKeyState(UIVirtualKeyboard.KeyState keyState)
		{
			UITextPanel<object> uitextPanel = null;
			UIVirtualKeyboard.KeyState keyState2 = this._keyState;
			if (keyState2 != UIVirtualKeyboard.KeyState.Symbol)
			{
				if (keyState2 == UIVirtualKeyboard.KeyState.Shift)
				{
					uitextPanel = this._shiftButton;
				}
			}
			else
			{
				uitextPanel = this._symbolButton;
			}
			if (uitextPanel != null)
			{
				if (uitextPanel.IsMouseHovering)
				{
					uitextPanel.BackgroundColor = new Color(73, 94, 171);
				}
				else
				{
					uitextPanel.BackgroundColor = new Color(63, 82, 151) * 0.7f;
				}
			}
			string text = null;
			UITextPanel<object> uitextPanel2 = null;
			switch (keyState)
			{
			case UIVirtualKeyboard.KeyState.Default:
				text = "1234567890qwertyuiopasdfghjkl'zxcvbnm,.?";
				break;
			case UIVirtualKeyboard.KeyState.Symbol:
				text = "1234567890!@#$%^&*()-_+=/\\{}[]<>;:\"`|~£¥";
				uitextPanel2 = this._symbolButton;
				break;
			case UIVirtualKeyboard.KeyState.Shift:
				text = "1234567890QWERTYUIOPASDFGHJKL'ZXCVBNM,.?";
				uitextPanel2 = this._shiftButton;
				break;
			}
			for (int i = 0; i < text.Length; i++)
			{
				this._keyList[i].SetText(text[i].ToString());
			}
			this._keyState = keyState;
			if (uitextPanel2 != null)
			{
				uitextPanel2.BackgroundColor = new Color(93, 114, 191);
			}
		}

		// Token: 0x060022FB RID: 8955 RVA: 0x00474BB0 File Offset: 0x00472DB0
		private void ValidateText()
		{
			if (this.Text.Trim().Length > 0 || this._edittingSign || this._edittingChest || this._allowEmpty)
			{
				this._canSubmit = true;
				this._submitButton.TextColor = Color.White;
				if (this._submitButton.IsMouseHovering)
				{
					this._submitButton.BackgroundColor = new Color(73, 94, 171);
					return;
				}
				this._submitButton.BackgroundColor = new Color(63, 82, 151) * 0.7f;
				return;
			}
			else
			{
				this._canSubmit = false;
				this._submitButton.TextColor = Color.Gray;
				if (this._submitButton.IsMouseHovering)
				{
					this._submitButton.BackgroundColor = new Color(180, 60, 60) * 0.85f;
					return;
				}
				this._submitButton.BackgroundColor = new Color(150, 40, 40) * 0.85f;
				return;
			}
		}

		// Token: 0x060022FC RID: 8956 RVA: 0x00474CB8 File Offset: 0x00472EB8
		private void StyleKey<T>(UITextPanel<T> button, bool external = false)
		{
			button.PaddingLeft = 0f;
			button.PaddingRight = 0f;
			button.BackgroundColor = new Color(63, 82, 151) * 0.7f;
			if (!external)
			{
				button.BorderColor = this._internalBorderColor * 0.7f;
			}
			button.OnMouseOver += delegate(UIMouseEvent evt, UIElement listeningElement)
			{
				((UITextPanel<T>)listeningElement).BackgroundColor = new Color(73, 94, 171) * 0.85f;
				if (!external)
				{
					((UITextPanel<T>)listeningElement).BorderColor = this._internalBorderColorSelected * 0.85f;
				}
			};
			button.OnMouseOut += delegate(UIMouseEvent evt, UIElement listeningElement)
			{
				((UITextPanel<T>)listeningElement).BackgroundColor = new Color(63, 82, 151) * 0.7f;
				if (!external)
				{
					((UITextPanel<T>)listeningElement).BorderColor = this._internalBorderColor * 0.7f;
				}
			};
		}

		// Token: 0x060022FD RID: 8957 RVA: 0x00474D50 File Offset: 0x00472F50
		private UITextPanel<object> CreateKeyboardButton(object text, int x, int y, int width = 1, bool style = true)
		{
			float num = 516f;
			UITextPanel<object> uitextPanel = new UITextPanel<object>(text, 0.4f, true);
			uitextPanel.Width.Pixels = 48f * (float)width + 4f * (float)(width - 1);
			uitextPanel.Height.Pixels = 37f;
			uitextPanel.Left.Precent = 0.5f;
			uitextPanel.Left.Pixels = 52f * (float)x - num * 0.5f;
			uitextPanel.Top.Pixels = 41f * (float)y;
			if (style)
			{
				this.StyleKey<object>(uitextPanel, false);
			}
			for (int i = 0; i < width; i++)
			{
				this._keyList[y * 10 + x + i] = uitextPanel;
			}
			return uitextPanel;
		}

		// Token: 0x060022FE RID: 8958 RVA: 0x00474E08 File Offset: 0x00473008
		protected override void DrawSelf(SpriteBatch spriteBatch)
		{
			if (Main.gameMenu)
			{
				if (PlayerInput.UsingGamepad)
				{
					this.outerLayer2.Remove();
					if (!this.Elements.Contains(this.outerLayer1))
					{
						base.Append(this.outerLayer1);
					}
					this.outerLayer1.Activate();
					this.outerLayer2.Deactivate();
					this.Recalculate();
					this.RecalculateChildren();
					if (this._labelHeight != 0f)
					{
						this._textBox.Top.Pixels = this._textBoxHeight;
						this._label.Top.Pixels = this._labelHeight;
						this._textBox.Recalculate();
						this._label.Recalculate();
						this._labelHeight = (this._textBoxHeight = 0f);
						UserInterface.ActiveInstance.ResetLasts();
					}
				}
				else
				{
					this.outerLayer1.Remove();
					if (!this.Elements.Contains(this.outerLayer2))
					{
						base.Append(this.outerLayer2);
					}
					this.outerLayer2.Activate();
					this.outerLayer1.Deactivate();
					this.Recalculate();
					this.RecalculateChildren();
					if (this._textBoxHeight == 0f)
					{
						this._textBoxHeight = this._textBox.Top.Pixels;
						this._labelHeight = this._label.Top.Pixels;
						UITextBox textBox = this._textBox;
						textBox.Top.Pixels = textBox.Top.Pixels + 50f;
						UIText label = this._label;
						label.Top.Pixels = label.Top.Pixels + 50f;
						this._textBox.Recalculate();
						this._label.Recalculate();
						UserInterface.ActiveInstance.ResetLasts();
					}
				}
			}
			if (!Main.editSign && this._edittingSign)
			{
				IngameFancyUI.Close();
				return;
			}
			if (!Main.editChest && this._edittingChest)
			{
				IngameFancyUI.Close();
				return;
			}
			base.DrawSelf(spriteBatch);
			this.UpdateOffsetDown();
			UIVirtualKeyboard.OffsetDown = 0;
			this.SetupGamepadPoints(spriteBatch);
			PlayerInput.WritingText = true;
			Main.instance.HandleIME();
			Vector2 position = new Vector2((float)(Main.screenWidth / 2), (float)(this._textBox.GetDimensions().ToRectangle().Bottom + 32));
			Main.instance.DrawWindowsIMEPanel(position, 0.5f);
			string text = Main.GetInputText(this.Text);
			if (this._edittingSign && Main.inputTextEnter)
			{
				text += "\n";
			}
			else
			{
				if (this._edittingChest && Main.inputTextEnter)
				{
					ChestUI.RenameChestSubmit(Main.player[Main.myPlayer]);
					IngameFancyUI.Close();
					return;
				}
				if (Main.inputTextEnter && UIVirtualKeyboard.CanSubmit)
				{
					UIVirtualKeyboard.Submit();
				}
				else if (Main.inputTextEscape)
				{
					if (this._edittingSign)
					{
						Main.InputTextSignCancel();
					}
					if (this._edittingChest)
					{
						ChestUI.RenameChestCancel();
					}
					IngameFancyUI.Close();
					return;
				}
			}
			if (IngameFancyUI.CanShowVirtualKeyboard(this._keyboardContext))
			{
				if (text != this.Text)
				{
					this.Text = text;
				}
				if (this._edittingSign)
				{
					this.CopyTextToSign();
				}
				if (this._edittingChest)
				{
					this.CopyTextToChest();
				}
			}
			byte b = (byte.MaxValue + Main.tileColor.R * 2) / 3;
			Color value = new Color((int)b, (int)b, (int)b, 255);
			this._textBox.TextColor = Color.Lerp(Color.White, value, 0.2f);
			this._label.TextColor = Color.Lerp(Color.White, value, 0.2f);
			position = new Vector2((float)(Main.screenWidth / 2), (float)(this._textBox.GetDimensions().ToRectangle().Bottom + 32));
			Main.instance.DrawWindowsIMEPanel(position, 0.5f);
		}

		// Token: 0x060022FF RID: 8959 RVA: 0x004751D8 File Offset: 0x004733D8
		private void UpdateOffsetDown()
		{
			int num = UIVirtualKeyboard.OffsetDown - this._lastOffsetDown;
			int num2 = num;
			if (Math.Abs(num) < 10)
			{
				num2 = num;
			}
			this._lastOffsetDown += num2;
			if (num2 == 0)
			{
				return;
			}
			this.Top.Pixels = this.Top.Pixels + (float)num2;
			this.Recalculate();
		}

		// Token: 0x06002300 RID: 8960 RVA: 0x00018B10 File Offset: 0x00016D10
		public override void OnActivate()
		{
			if (PlayerInput.UsingGamepadUI && this._restoreButton != null)
			{
				UILinkPointNavigator.ChangePoint(3002);
			}
		}

		// Token: 0x06002301 RID: 8961 RVA: 0x00018B2B File Offset: 0x00016D2B
		public override void OnDeactivate()
		{
			base.OnDeactivate();
			PlayerInput.WritingText = false;
			UILinkPointNavigator.Shortcuts.FANCYUI_SPECIAL_INSTRUCTIONS = 0;
		}

		// Token: 0x06002302 RID: 8962 RVA: 0x00475234 File Offset: 0x00473434
		private void SetupGamepadPoints(SpriteBatch spriteBatch)
		{
			UILinkPointNavigator.Shortcuts.BackButtonCommand = 6;
			UILinkPointNavigator.Shortcuts.FANCYUI_SPECIAL_INSTRUCTIONS = 1;
			int num = 3002;
			UILinkPointNavigator.SetPosition(3000, this._cancelButton.GetDimensions().Center());
			UILinkPoint uilinkPoint = UILinkPointNavigator.Points[3000];
			uilinkPoint.Unlink();
			uilinkPoint.Right = 3001;
			uilinkPoint.Up = num + 40;
			UILinkPointNavigator.SetPosition(3001, this._submitButton.GetDimensions().Center());
			uilinkPoint = UILinkPointNavigator.Points[3001];
			uilinkPoint.Unlink();
			uilinkPoint.Left = 3000;
			uilinkPoint.Up = num + 49;
			for (int i = 0; i < 5; i++)
			{
				for (int j = 0; j < 10; j++)
				{
					int num2 = i * 10 + j;
					int num3 = num + num2;
					if (this._keyList[num2] != null)
					{
						UILinkPointNavigator.SetPosition(num3, this._keyList[num2].GetDimensions().Center());
						uilinkPoint = UILinkPointNavigator.Points[num3];
						uilinkPoint.Unlink();
						int num4 = j - 1;
						while (num4 >= 0 && this._keyList[i * 10 + num4] == this._keyList[num2])
						{
							num4--;
						}
						if (num4 != -1)
						{
							uilinkPoint.Left = i * 10 + num4 + num;
						}
						else
						{
							uilinkPoint.Left = i * 10 + 9 + num;
						}
						int num5 = j + 1;
						while (num5 <= 9 && this._keyList[i * 10 + num5] == this._keyList[num2])
						{
							num5++;
						}
						if (num5 != 10 && this._keyList[num2] != this._keyList[num5])
						{
							uilinkPoint.Right = i * 10 + num5 + num;
						}
						else
						{
							uilinkPoint.Right = i * 10 + num;
						}
						if (i != 0)
						{
							uilinkPoint.Up = num3 - 10;
						}
						if (i != 4)
						{
							uilinkPoint.Down = num3 + 10;
						}
						else
						{
							uilinkPoint.Down = ((j < 5) ? 3000 : 3001);
						}
					}
				}
			}
		}

		// Token: 0x06002303 RID: 8963 RVA: 0x00475444 File Offset: 0x00473644
		public static void CycleSymbols()
		{
			if (UIVirtualKeyboard._currentInstance == null)
			{
				return;
			}
			switch (UIVirtualKeyboard._currentInstance._keyState)
			{
			case UIVirtualKeyboard.KeyState.Default:
				UIVirtualKeyboard._currentInstance.SetKeyState(UIVirtualKeyboard.KeyState.Shift);
				return;
			case UIVirtualKeyboard.KeyState.Symbol:
				UIVirtualKeyboard._currentInstance.SetKeyState(UIVirtualKeyboard.KeyState.Default);
				return;
			case UIVirtualKeyboard.KeyState.Shift:
				UIVirtualKeyboard._currentInstance.SetKeyState(UIVirtualKeyboard.KeyState.Symbol);
				return;
			default:
				return;
			}
		}

		// Token: 0x06002304 RID: 8964 RVA: 0x00018B3F File Offset: 0x00016D3F
		public static void BackSpace()
		{
			if (UIVirtualKeyboard._currentInstance == null)
			{
				return;
			}
			Main.PlaySound(12, -1, -1, 1, 1f, 0f);
			UIVirtualKeyboard._currentInstance._textBox.Backspace();
			UIVirtualKeyboard._currentInstance.ValidateText();
		}

		// Token: 0x06002305 RID: 8965 RVA: 0x0047549C File Offset: 0x0047369C
		public static void Submit()
		{
			if (UIVirtualKeyboard._currentInstance == null)
			{
				return;
			}
			string text = UIVirtualKeyboard._currentInstance.Text.Trim();
			if (text.Length > 0)
			{
				Main.PlaySound(10, -1, -1, 1, 1f, 0f);
				UIVirtualKeyboard._currentInstance._submitAction(text);
			}
		}

		// Token: 0x06002306 RID: 8966 RVA: 0x00018B77 File Offset: 0x00016D77
		public static void Cancel()
		{
			if (UIVirtualKeyboard._currentInstance == null)
			{
				return;
			}
			Main.PlaySound(11, -1, -1, 1, 1f, 0f);
			UIVirtualKeyboard._currentInstance._cancelAction();
		}

		// Token: 0x06002307 RID: 8967 RVA: 0x004754F0 File Offset: 0x004736F0
		public static void Write(string text)
		{
			if (UIVirtualKeyboard._currentInstance == null)
			{
				return;
			}
			Main.PlaySound(12, -1, -1, 1, 1f, 0f);
			bool flag = UIVirtualKeyboard._currentInstance.Text.Length == 0;
			UIVirtualKeyboard._currentInstance._textBox.Write(text);
			UIVirtualKeyboard._currentInstance.ValidateText();
			if (flag && UIVirtualKeyboard._currentInstance.Text.Length > 0 && UIVirtualKeyboard._currentInstance._keyState == UIVirtualKeyboard.KeyState.Shift)
			{
				UIVirtualKeyboard._currentInstance.SetKeyState(UIVirtualKeyboard.KeyState.Default);
			}
		}

		// Token: 0x06002308 RID: 8968 RVA: 0x00018BA5 File Offset: 0x00016DA5
		public static void CursorLeft()
		{
			if (UIVirtualKeyboard._currentInstance == null)
			{
				return;
			}
			Main.PlaySound(12, -1, -1, 1, 1f, 0f);
			UIVirtualKeyboard._currentInstance._textBox.CursorLeft();
		}

		// Token: 0x06002309 RID: 8969 RVA: 0x00018BD3 File Offset: 0x00016DD3
		public static void CursorRight()
		{
			if (UIVirtualKeyboard._currentInstance == null)
			{
				return;
			}
			Main.PlaySound(12, -1, -1, 1, 1f, 0f);
			UIVirtualKeyboard._currentInstance._textBox.CursorRight();
		}

		// Token: 0x0600230A RID: 8970 RVA: 0x00018C01 File Offset: 0x00016E01
		public static bool CanDisplay(int keyboardContext)
		{
			return keyboardContext != 1 || Main.screenHeight > 700;
		}

		// Token: 0x0600230B RID: 8971 RVA: 0x00018C15 File Offset: 0x00016E15
		public static void CacheCancelledInput(int cacheMode)
		{
			if (cacheMode != 1)
			{
				return;
			}
			UIVirtualKeyboard._cancelCacheSign = Main.npcChatText;
		}

		// Token: 0x0600230C RID: 8972 RVA: 0x00018C26 File Offset: 0x00016E26
		private void RestoreCancelledInput(int cacheMode)
		{
			if (cacheMode != 1)
			{
				return;
			}
			Main.npcChatText = UIVirtualKeyboard._cancelCacheSign;
			this.Text = Main.npcChatText;
			UIVirtualKeyboard._cancelCacheSign = "";
		}

		// Token: 0x0600230D RID: 8973 RVA: 0x00475574 File Offset: 0x00473774
		private void CopyTextToSign()
		{
			if (!this._edittingSign)
			{
				return;
			}
			int sign = Main.player[Main.myPlayer].sign;
			if (sign < 0 || Main.sign[sign] == null)
			{
				return;
			}
			Main.npcChatText = this.Text;
		}

		// Token: 0x0600230E RID: 8974 RVA: 0x00018C4C File Offset: 0x00016E4C
		private void CopyTextToChest()
		{
			if (!this._edittingChest)
			{
				return;
			}
			Main.npcChatText = this.Text;
		}

		// Token: 0x04003F4D RID: 16205
		private const string DEFAULT_KEYS = "1234567890qwertyuiopasdfghjkl'zxcvbnm,.?";

		// Token: 0x04003F4E RID: 16206
		private const string SHIFT_KEYS = "1234567890QWERTYUIOPASDFGHJKL'ZXCVBNM,.?";

		// Token: 0x04003F4F RID: 16207
		private const string SYMBOL_KEYS = "1234567890!@#$%^&*()-_+=/\\{}[]<>;:\"`|~£¥";

		// Token: 0x04003F50 RID: 16208
		private const float KEY_SPACING = 4f;

		// Token: 0x04003F51 RID: 16209
		private const float KEY_WIDTH = 48f;

		// Token: 0x04003F52 RID: 16210
		private const float KEY_HEIGHT = 37f;

		// Token: 0x04003F53 RID: 16211
		private static UIVirtualKeyboard _currentInstance;

		// Token: 0x04003F54 RID: 16212
		private static string _cancelCacheSign = "";

		// Token: 0x04003F55 RID: 16213
		private static string _cancelCacheChest = "";

		// Token: 0x04003F56 RID: 16214
		private UITextPanel<object>[] _keyList = new UITextPanel<object>[50];

		// Token: 0x04003F57 RID: 16215
		private UITextPanel<object> _shiftButton;

		// Token: 0x04003F58 RID: 16216
		private UITextPanel<object> _symbolButton;

		// Token: 0x04003F59 RID: 16217
		private UITextBox _textBox;

		// Token: 0x04003F5A RID: 16218
		private UITextPanel<LocalizedText> _submitButton;

		// Token: 0x04003F5B RID: 16219
		private UITextPanel<LocalizedText> _cancelButton;

		// Token: 0x04003F5C RID: 16220
		private UIText _label;

		// Token: 0x04003F5D RID: 16221
		private UITextPanel<object> _enterButton;

		// Token: 0x04003F5E RID: 16222
		private UITextPanel<object> _spacebarButton;

		// Token: 0x04003F5F RID: 16223
		private UITextPanel<object> _restoreButton;

		// Token: 0x04003F60 RID: 16224
		private Texture2D _textureShift;

		// Token: 0x04003F61 RID: 16225
		private Texture2D _textureBackspace;

		// Token: 0x04003F62 RID: 16226
		private Color _internalBorderColor = new Color(89, 116, 213);

		// Token: 0x04003F63 RID: 16227
		private Color _internalBorderColorSelected = Main.OurFavoriteColor;

		// Token: 0x04003F64 RID: 16228
		private UITextPanel<LocalizedText> _submitButton2;

		// Token: 0x04003F65 RID: 16229
		private UITextPanel<LocalizedText> _cancelButton2;

		// Token: 0x04003F66 RID: 16230
		private UIElement outerLayer1;

		// Token: 0x04003F67 RID: 16231
		private UIElement outerLayer2;

		// Token: 0x04003F68 RID: 16232
		private bool _allowEmpty;

		// Token: 0x04003F69 RID: 16233
		private UIVirtualKeyboard.KeyState _keyState;

		// Token: 0x04003F6A RID: 16234
		private UIVirtualKeyboard.KeyboardSubmitEvent _submitAction;

		// Token: 0x04003F6B RID: 16235
		private Action _cancelAction;

		// Token: 0x04003F6C RID: 16236
		private int _lastOffsetDown;

		// Token: 0x04003F6D RID: 16237
		public static int OffsetDown = 0;

		// Token: 0x04003F6E RID: 16238
		private int _keyboardContext;

		// Token: 0x04003F6F RID: 16239
		private bool _edittingSign;

		// Token: 0x04003F70 RID: 16240
		private bool _edittingChest;

		// Token: 0x04003F71 RID: 16241
		private float _textBoxHeight;

		// Token: 0x04003F72 RID: 16242
		private float _labelHeight;

		// Token: 0x04003F73 RID: 16243
		private bool _canSubmit;

		// Token: 0x020003D9 RID: 985
		// (Invoke) Token: 0x06002320 RID: 8992
		public delegate void KeyboardSubmitEvent(string text);

		// Token: 0x020003DA RID: 986
		public enum KeyState
		{
			// Token: 0x04003F75 RID: 16245
			Default,
			// Token: 0x04003F76 RID: 16246
			Symbol,
			// Token: 0x04003F77 RID: 16247
			Shift
		}
	}
}
